const setStatus = require("../common/setStatus");
const checkLogin = require("../common/checkLogin");
const { query, invoke} = require('../tools/index.js');

const invokeOptions = {
    host: "120.79.19.57",
    orderHost:"139.199.191.238",
    org:"3",
    username: "seller33",
    chaincodeId: "seller",
    channelId: "mychannel",
    fcn: null,
    args: [],
}

const queryOptions = {
    username: "seller33",
    chaincodeId: "seller",
    fcn: null,
    args: [],
    host: '120.79.19.57',
    org: '3'
}

function setQuery(fcn, args) {
    return Object.assign(queryOptions, {fcn, args});
}

function setInvoke(fcn, args) {
    return Object.assign(invokeOptions, {fcn, args});
}

module.exports = {
    createUser: async (ctx) => {
        // checkLogin(ctx);
        const {UserId, Password, Role, Org, Add, Edit, Delete} = ctx.request.body;
        const data = await query(setQuery("query", [UserId]));
      
        if(!UserId||!Password||!Role||!Org) {
            setStatus(ctx, 200,0, "", "缺少参数");
            return;
        }
        const args = [UserId, Password, Role, Org, Add, Edit, Delete];
        await invoke(setInvoke("createUser", [...args]));
        setStatus(ctx, 200, 1, "");
    },
    getUser: async (ctx) => {
        // checkLogin(ctx);
        const {UserId} = ctx.request.query;
        if (!UserId) {
            setStatus(ctx, 200,0,"","无请求参数id");
            return;
        }
        const data = await query(setQuery("query", [UserId]));
        setStatus(ctx, 200, 1, data);
    },
    deleteUser: async (ctx) => {
        const {UserId} = ctx.request.body;
        if(!UserId) {
            setStatus(ctx, 200,0, "", "缺少参数");
            return;
        }
        const args = [UserId];
        await invoke(setInvoke("delete", [...args]));
        setStatus(ctx, 200, 1, "");
    },
};